package com.atguigu.lease.web.app.custom.config;

import com.atguigu.lease.web.app.custom.converter.StringToBaseEnumConverterFactory;
import com.atguigu.lease.web.app.custom.interceptor.AuthenticationInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * ClassName: WebMvcConfiguration
 * Package: com.atguigu.lease.web.app.custom.config
 * Description:
 * 配置拦截器
 * @Autor: xvyy
 * @Create: 2024/11/21 - 19:39
 * @Version: v1.0
 */
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {
    @Autowired
    private AuthenticationInterceptor authenticationInterceptor;
    @Autowired
    private StringToBaseEnumConverterFactory stringToBaseEnumConverterFactory;

    @Value("${app.auth.path-patterns.include}")
    private String[] includePathPatterns;
    @Value("${app.auth.path-patterns.exclude}")
    private String[] excludePathPatterns;

    /**
     * 将字符串转换为ItemType枚举类型
     *
     * @param registry
     */
    @Override
    public void addFormatters(FormatterRegistry registry) {
        //todo 注册自定义类型转换器 : 用于将字符串转换为ItemType枚举类型
//        registry.addConverter(/*this.*/stringToItemTypeConverter);
        registry.addConverterFactory(stringToBaseEnumConverterFactory);
    }

    //todo 配置拦截器

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authenticationInterceptor).addPathPatterns(includePathPatterns).excludePathPatterns(excludePathPatterns);
    }
}
